package com.webservice.impl.returnDeal;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.webservice.dao.Pub;
import com.webservice.dao.PubBaseDao;
import com.webservice.dao.returnDeal.IreturnDealDao;
import com.webservice.service.IBaseService;
import com.webservice.utils.DateTimeTool;
import com.webservice.utils.UUIDUtil;
import com.webservice.utils.VOXMLMapping;
import com.webservice.vo.impl.ReturnMsgVo;
import com.webservice.vo.returnDeal.OrderReturnStoresVo;
import com.webservice.vo.returnDeal.ProductVo;
import com.webservice.vo.returnDeal.ReturnSingleDetailVo;
import com.webservice.vo.returnDeal.ReturnSingleVo;

/**
 * 返回单信息回传
 * @author chxb
 * B1005
 */
@Service("B1005")
public class ReturnDealStore implements IBaseService{

	private static Logger logger = Logger.getLogger(ReturnDealStore.class);
	
	@Autowired
	@Qualifier("ReturnDealDao")
	private IreturnDealDao returnDealDao;
	

	/***
	 * 1.解析xml文件
	 * 2.插入请求日志
	 * 3.请求处理
	 * 4.数据校验
	 * 5.返回日志记录
	 * @throws Exception 
	 */
	@Override
	@Transactional(value = "txManager", isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED, rollbackFor = { Exception.class })
	public String returnData(String xml, PubBaseDao baseDao, String ip) throws Exception {
		OrderReturnStoresVo returnVo=null;
		String intoUUID = UUIDUtil.getUUID();
		String outUUID = UUIDUtil.getUUID();
		String returnxml=null;
		String content = null;
		// 成对标志
		String pairflag = DateTimeTool.getDateTimeString(new Date(),"yyyymmddHHmmss:sss").replace(":", "");
		//xml转换成对应的VO
		try{
			returnVo=(OrderReturnStoresVo) VOXMLMapping.fromXML(xml,OrderReturnStoresVo.class, null);
			//日志记录
			Pub.insertLog(baseDao, intoUUID, ip, "20", returnVo.getOrdercode(), xml, "into", pairflag,"0");
			//返还单具体明细信息存储
			boolean flag=getReturnDealDao().insertReturnSingle(getReturnSingle(returnVo));
			if(flag){
				Map<String,String> map=new HashMap<String,String>();
				map.put("code", "ordercode");
				returnxml = Pub.returnInfo(returnVo.getOrdercode(),0,"成功",map);
			}else{	
				returnxml=Pub.returnInfo(returnVo.getOrdercode(), -1,"失败：返还单信息存储失败！", null);
			}
			//返回日志信息记录
			Pub.insertLog(baseDao, outUUID, ip,"20", returnVo.getOrdercode(), returnxml,"out", pairflag,"1");
			ReturnMsgVo rmv =  (ReturnMsgVo) VOXMLMapping.fromXML(returnxml, ReturnMsgVo.class, null);
			if(rmv.getResult() == 0){
				Pub.insertDealResultLog(baseDao, outUUID, "WMS", "B2B", "成功", "WMS返还单通知B2B成功！");
			}else{
				Pub.insertDealResultLog(baseDao, outUUID, "WMS", "B2B", "处理异常", rmv.getMsg());
			}
			return  returnxml;
		}catch(Exception e){
			e.printStackTrace();
			content = "请求的xml转化错误" + e.getMessage();
			Pub.insertLog(baseDao, intoUUID, ip, "20", returnVo.getOrdercode()==null?null:returnVo.getOrdercode(), xml, "into", pairflag,"0");
			returnxml = Pub.returnInfo(null, -1, "失败：" + content, null);
			Pub.insertLog(baseDao, outUUID, ip, "20", returnVo.getOrdercode()==null?null:returnVo.getOrdercode(), returnxml, "out", pairflag,"1");
			throw new Exception(returnxml);
		}
	}
	
	/**
	 * 根据返还单xml信息查找返还单信息
	 * @param OrderReturnStoresVo vo
	 * @return ReturnSingleVo 返还单VO
	 */
	public  ReturnSingleVo getReturnSingle(OrderReturnStoresVo vo){
		if(vo==null)
			return null;
		List<ProductVo> listproduct=vo.getProducts();
		ReturnSingleVo returnSingleVo = new ReturnSingleVo();
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
		returnSingleVo.setSingleCode("RS"+System.currentTimeMillis());
		returnSingleVo.setAddDate(sf.format(new Date()));
		returnSingleVo.setSingleId(UUIDUtil.getUUID());
		//0:已删除 1：正常
		returnSingleVo.setDr("1");
		/**返还单状态 2：已退还*/
		returnSingleVo.setStatus("2");
		returnSingleVo.setUpdateDate(sf.format(new Date()));
		returnSingleVo.setUpdateUser("1");
		returnSingleVo.setAddUser("1");
		returnSingleVo.setSupplyName(vo.getSupplyname());
		returnSingleVo.setSupplyId(vo.getSupplyid());
		returnSingleVo.setSupplyTel(vo.getSupplytel());
		returnSingleVo.setSupplyAddr(vo.getSupplyaddr());
		//1:好机汇 2：天联网
		returnSingleVo.setType(vo.getType());
		returnSingleVo.setOrderCode(vo.getOrdercode());
		//取出返还单中产品的信息
		List<ReturnSingleDetailVo> list=new ArrayList<ReturnSingleDetailVo>();
		for(int i=0;i<listproduct.size();i++){
			ReturnSingleDetailVo detailVo=new ReturnSingleDetailVo();
			detailVo.setSingleDetailId(UUIDUtil.getUUID());
			detailVo.setProductId(listproduct.get(i).getProductcode());
			detailVo.setProductName(listproduct.get(i).getProductname());
			detailVo.setReturnNumber(listproduct.get(i).getReturnquantity());
			list.add(detailVo);
		}
		returnSingleVo.setList(list);
		return returnSingleVo;
	}
	
	
	
	
	public IreturnDealDao getReturnDealDao() {
		return returnDealDao;
	}

	public void setReturnDealDao(IreturnDealDao returnDealDao) {
		this.returnDealDao = returnDealDao;
	}


}
